package com.mjp.learn.multi.task;

/**
 * 两个线程相继打印
 * 用wait和notify
 */
public class Test01 {

    public static volatile Object object = new Object();

    static class Thread01 implements Runnable{
        @Override
        public void run() {
            synchronized (object) {
                for(int i = 1 ; i <= 26; i ++){
                    System.out.print(i);
                    try {
                        object.notify();
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                object.notify();
            }
        }
    }

    static class Thread02 implements Runnable{

        @Override
        public void run() {
            synchronized (object) {
                for(char i = 'A' ; i <='Z'; i++){
                    System.out.print(i);
                    try {
                        object.notify();
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                object.notify();
            }
        }
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(new Thread01(),"t1");
        Thread thread2 = new Thread(new Thread02(),"t2");
        thread1.start();
        thread2.start();
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(" ");
        System.out.println("program end");
    }
}
